<?php
namespace app\api\controller\v1;
use app\api\controller\Common;

class MemberFans extends Common{
	var $modelDb;
	public function _initialize(){
		parent::_initialize();
		$this->modelDb = model("MemberFans");
	}
	public function add(){
		$this->postData['userid'] = $this->uinfo['id'];
		$result = $this->modelDb->get_relat($this->postData['userid'], $this->postData['touserid']);
        if ($result === 1) {
            return $this->app_error('你们已经是好友了');
        }
        elseif ($result === 2) {
            return $this->app_error('你已经发送过好友申请');
        }elseif ($result === 3) {
			$id = $this->modelDb->set_status($this->postData);

		}elseif (!$result){
            $id = $this->modelDb->save_data($this->postData);
            send_rongyun_sys_msg($this->postData['touserid'],$this->uinfo['realname'].'想添加你为好友', 0, 0);
        }
        if(isset($id) && $id === false){
            return $this->app_error($this->modelDb->getError());
        }
    	return $this->app_success();
    }
    public function index(){
    	$where['userid|touserid'] = $this->uinfo['id'];
    	$where['status'] = 1;
    	$data = $this->modelDb->list_data($where);
    	$datas = [];
    	foreach ($data as $v) {
    		$touserid = $this->uinfo['id'] == $v['userid'] ? $v['touserid'] : $v['userid']; 
    		$info = model("Member")->get_data(['item_id'=>$touserid]); 
            if (!empty($info)) {
                if (!$info['realname']) {
                    $info['realname'] = model("Member")->get_member_realname($touserid);
                }
                $datas[] = $info;
            }  		
    	}
    	if($datas){
    		return $this->app_success($datas);
    	}else{
    		return $this->app_success([],"好友为空");
    	}
    }
    //同意好友申请
    public function applypass(){
        $this->postData['userid'] = $this->uinfo['id'];
        $id = $this->modelDb->set_status($this->postData);
        if (!$id) {
            return $this->app_error($this->modelDb->getError());
        }else{
            return $this->app_success([], '申请成功');
        }
    }
    //拒绝好友申请
    public function refusepass(){
        $this->postData['userid'] = isset($this->uinfo['id']) ? $this->uinfo['id'] : 1;
        $id = $this->modelDb->refuse($this->postData);
        if (!$id) {
            return $this->app_error($this->modelDb->getError());
        }else{
            return $this->app_success([], '成功拒绝');
        }
    }
    public function delfans(){
        $this->postData['userid'] = $this->uinfo['id'];
        $result = $this->validate($this->postData, 'MemberFans.check_type');
        if ($result !== true) {
            return $this->app_error($result);
        }
        $ids = $this->modelDb->where(function ($query) {
            $query->where('userid', $this->postData['userid'])->where('touserid', $this->postData['touserid']);
        })->whereOr(function ($query) {
            $query->where('userid', $this->postData['touserid'])->where('touserid', $this->postData['userid']);
        })->delete();
        if ($ids) {
            return $this->app_success();
        }else{
            return $this->app_error();
        }
    }
    //等待申请好友列表
    public function applylist(){
        $userid = isset($this->uinfo['id']) ? $this->uinfo['id'] : 1;
        $page = isset($this->postData['page']) && $this->postData['page'] ? $this->postData['page'] : 1;
        $offset = ($page - 1) * 20; 
        $list = $this->modelDb->list_data(['touserid'=>$userid, 'status'=>0], 20, $offset, 'id, userid');
        $lists = [];
        foreach ($list as $v) {
            $info = model("Member")->get_data(['item_id'=>$v['userid']], 'id, username, nickname, realname, phone, avatar'); 
            if (!empty($info)) {
                $r['id'] = $info['id'];
                $r['touserid'] = $info['id'];
                $r['realname'] = $info['realname'];
                $r['phone'] = $info['phone'];
                $r['avatar'] = $info['avatar'];
                $r['relation'] = 3;
                $lists[] = $r;           
            }       
        }
        if (!empty($list)) {
            return $this->app_success($lists);
        }else{
            return $this->app_success();
        }
    }
}